package com.sanerzone.smscenter.modules.sms.web.front;

import com.sanerzone.jeebase.common.utils.DateUtils;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.smscenter.modules.sms.dao.SmsSendDao;
import com.sanerzone.smscenter.modules.sms.entity.*;
import com.sanerzone.smscenter.modules.sms.web.util.ControllerUtil;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/front/jump")
public class FrontJumpController {

    @Autowired
    private SmsSendDao smsSendDao;

    @RequestMapping(value = "sendedPage")
    public String sendedRecord(String id,Date sendDate,String date,Model model,String currentPageNo,String perPageCount){
         Map map = new HashMap();
         map.put( "userId", id );
         int count = smsSendDao.getSubmitCountByUserId( map );
         Page page = new Page();
         if (perPageCount==null){
             //默认5个
             page.setPerPageCount( 5 );
         }else{
             page.setPerPageCount( Integer.parseInt( perPageCount ) );
         }if (currentPageNo==null){
             page.setCurrentPageNo( 1 );
        }else{
            page.setCurrentPageNo( Integer.parseInt( currentPageNo ) );
        }
         page.setTotalItemCount( count );

         map.put( "start", page.getStartIndex() );
         map.put( "end",page.getPerPageCount() );
         List<SubmitStatistics> submitStatistics=  smsSendDao.getStatustics( map );
        model.addAttribute( "records",submitStatistics );
        model.addAttribute( "page",page );

        return "modules/sms/front/smsSendRecord";
    }

    @RequestMapping(value = "orderList")
    public String orderList(String submitId,String userId,Model model,String currentPageNo,String perPageCount,String phone,String content){
        String message="";
        String tableIndex=getTableIndex(submitId);
        if (tableIndex==null){
            message="查找有误，请联系客服。";
        }
        if (StringUtils.isBlank( currentPageNo )){
            currentPageNo="1";
        }
        if (StringUtils.isBlank( perPageCount )){
            perPageCount="5";
        }

        Map map=new HashMap( );
        map.put( "submitId",submitId );
        map.put( "userId",userId );
        map.put( "tableName",tableIndex );
        if (StringUtils.isNotBlank( phone )){
            map.put( "phone","%"+phone+"%" );
        }
        if (StringUtils.isNotBlank( content )){
            map.put( "content","%"+content+"%" );
        }

        int count=smsSendDao.getCountByTaskIdAndUserId( map );

        Page page=new Page();
        page.setPerPageCount( Integer.parseInt( perPageCount ) );
        page.setTotalItemCount( count );
        if (!StringUtils.isBlank( content )||!StringUtils.isBlank( phone )){
            page.setCurrentPageNo( 1 );
        }else {
            page.setCurrentPageNo( Integer.parseInt( currentPageNo ) );
        }
        map.put( "start",page.getStartIndex() );
        map.put( "end",page.getPerPageCount() );

        List<SmsSend> list=smsSendDao.getSmsSendListByUserIdAndTaskId( map );
        List<SmsDetail> details= ControllerUtil.smsSend2SmsDetail(list);

        model.addAttribute( "list",details );
        model.addAttribute( "page",page );
        model.addAttribute( "message",message );
        model.addAttribute( "taskid",submitId );
        model.addAttribute( "content",content );
        model.addAttribute( "phone",phone );

        return "modules/sms/front/smsSendRecordList";
    }


    public String jumpPageList(String totalItemCount,String totalPageNum,String currentPageNo,String perPageCount,String id,String sendDate,Model model,String
            taskid){
        String message="";
        String tableIndex=getTableIndex(taskid);
        if (tableIndex==null){
            message="查找有误，请联系客服。";

        }
        Map map=new HashMap( );
        map.put( "taskId",taskid );
        map.put( "userId",id );
        map.put( "tableName",tableIndex );
        int count=smsSendDao.getCountByTaskIdAndUserId( map );
        Page page=new Page();
        page.setPerPageCount( Integer.parseInt( perPageCount ) );
        page.setTotalItemCount( count );
        page.setCurrentPageNo( Integer.parseInt( currentPageNo ) );
        map.put( "start",page.getStartIndex() );
        map.put( "end",page.getStartIndex()+page.getPerPageCount() );
        List list=smsSendDao.getSmsSendListByUserIdAndTaskId( map );

        model.addAttribute( "list",list );
        model.addAttribute( "page",page );
        model.addAttribute( "message",message );
        model.addAttribute( "taskid",taskid );

        return "modules/sms/front/smsSendRecordList";

    }



    @RequestMapping(value = "jumpPage")
    public String jumpPage(String totalPageNum,String currentPageNo,String perPageCount,String id,String sendDate,Model model){
        Date date=null;
        if (sendDate==null){
            date=new Date( );
        }else{
            SimpleDateFormat sdf=new SimpleDateFormat( "yyyy-MM-dd" );
            try{
                date=sdf.parse( sendDate );
            }catch (Exception e){
                date=new Date( );
            }
        }
        String tableIndex=getTableIndex( date );
        Map map=new HashMap( );
        map.put( "tableName",tableIndex );
        map.put( "userId",id );
        Page page=new Page();
        page.setPerPageCount( Integer.parseInt( perPageCount ) );
        int count=smsSendDao.userTaskCount( map );
        page.setTotalItemCount(count);
        page.setCurrentPageNo( Integer.parseInt( currentPageNo ) );
        map.put( "start",page.getStartIndex() );
        map.put( "end",page.getPerPageCount() );
        List<TaskSort> taskSorts= smsSendDao.findTaskSorts( map );
        model.addAttribute( "list",taskSorts );
        model.addAttribute( "page",page );
        return "modules/sms/front/smsSendRecord";
    }




    public String getTableIndex(Date sendDate){
        String tableIndex="";
        SimpleDateFormat sdfYear=new SimpleDateFormat( "yyyy");
        SimpleDateFormat sdfMonth=new SimpleDateFormat( "MM");
        SimpleDateFormat sdfDay=new SimpleDateFormat( "dd");
        Date nowDate=new Date();
        int offset=(int)(sendDate.getTime()-nowDate.getTime())/1000/60/60/24;
        if (offset>=-4){
            //4天以内  直接去找日表

            String sdfDaySearch=sdfDay.format( sendDate );
            tableIndex="sms_send_"+sdfDaySearch;
        }else {
            String year=sdfYear.format( sendDate );
            String month=sdfMonth.format( sendDate );
            //4天以外
            tableIndex="sms_send_history_"+year+month;

        }

        return tableIndex;
    }
    //根据任务id 找到表
    public String getTableIndex(String taskId){
        String tableIndex=null;
        try{
            Date date=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("MMdd" );
            SimpleDateFormat sdfDay=new SimpleDateFormat( "dd" );
            String nowDateStr= sdf.format( date );

            Date  chooseDate=  sdf.parse( taskId.substring( 0,4 ));
            int offset=(int)(chooseDate.getTime()-date.getTime())/1000/60/60/24;
            if (offset>=-4){
                //4天以内  直接去找日表
                String sdfDaySearch=sdfDay.format( chooseDate );
                if(sdfDaySearch.charAt( 0 )=='0'){
                    sdfDaySearch=sdfDaySearch.charAt( 1 )+"";
                }

                tableIndex="sms_send_"+sdfDaySearch;


            }else {
                String month=taskId.substring( 0,2 );
                tableIndex="sms_send_history_"+"2018"+month;

            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return tableIndex;
    }


}
